package cn.stylefeng.guns.modular.guide.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.guide.entity.UserGuide;
import cn.stylefeng.guns.modular.guide.mapper.UserGuideMapper;
import cn.stylefeng.guns.modular.guide.model.params.UserGuideParam;
import cn.stylefeng.guns.modular.guide.model.result.UserGuideResult;
import  cn.stylefeng.guns.modular.guide.service.UserGuideService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户指南 服务实现类
 * </p>
 *
 * @author liuqiang
 * @since 2020-02-12
 */
@Service
public class UserGuideServiceImpl extends ServiceImpl<UserGuideMapper, UserGuide> implements UserGuideService {

    @Override
    public void add(UserGuideParam param){
        UserGuide entity = getEntity(param);
        UserGuide oldEntity = getOldEntity(param);
        if(oldEntity != null){
            this.updateById(entity);
        }else {
            this.save(entity);
        }
    }

    @Override
    public void delete(UserGuideParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserGuideParam param){
        UserGuide oldEntity = getOldEntity(param);
        UserGuide newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserGuideResult findBySpec(UserGuideParam param){
        return null;
    }

    @Override
    public List<UserGuideResult> findListBySpec(UserGuideParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserGuideParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(UserGuideParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserGuide getOldEntity(UserGuideParam param) {
        return this.getById(getKey(param));
    }

    private UserGuide getEntity(UserGuideParam param) {
        UserGuide entity = new UserGuide();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
